﻿/*
 * В рамках данного класса происходит работа с камерой и ветвление алгоритма работы
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using WebCam_Capture;

namespace DiplomNN
{
    class WebCam
    {
        #region Поля класса
        private WebCamCapture camera;
        private int frameNumber = 1;
        private Bitmap capture;


        //int flag = 0;
       
        # endregion

        public void InitialWebCam()
        {
            camera = new WebCamCapture();
            camera.FrameNumber = ((ulong)(0ul));
            camera.TimeToCapture_milliseconds = frameNumber;            
            camera.ImageCaptured += new WebCamCapture.WebCamEventHandler(WebCamCapturedMode);            
        }

        void WebCamCapturedMode(object source, WebcamEventArgs e)
        {
            capture = new Bitmap(e.WebCamImage);
            byte[, ,] RGBImage = Colors.BitmapToByteRgb(capture);
            Helper.ImageFromStartFormVideoPB = capture;
            if (Helper.IsActiveColorsSettingsForm)
            {
                byte[, ,] FilteredRGBImage = BodyRecogn.RGBDetection(Colors.BitmapToByteRgb(capture));
                byte[, ,] FilteredHSVImage = BodyRecogn.HSVDetection(Colors.BitmapToByteRgb(capture));
                byte[, ,] FilteredYCbCrImage = BodyRecogn.YCbCrDetection(Colors.BitmapToByteRgb(capture));
                
                Helper.ImageFilteredRGBPB = Colors.RgbToBitmap(FilteredRGBImage);
                Helper.ImageFilteredHSVPB = Colors.RgbToBitmap(FilteredHSVImage);
                Helper.ImageFilteredYCbCrPB = Colors.RgbToBitmap(FilteredYCbCrImage);
            }
            else
            {
                switch (Helper.MethodNumberColorsSettingsForm)
                {
                    case 1:
                        {
                            byte[, ,] FilteredRGBImage = BodyRecogn.RGBDetection(Colors.BitmapToByteRgb(capture));
                            Helper.ImageFromStartFormFiltered = Colors.RgbToBitmap(FilteredRGBImage);
                            break;
                        }
                    case 2:
                        {
                            byte[, ,] FilteredHSVImage = BodyRecogn.HSVDetection(Colors.BitmapToByteRgb(capture));
                            Helper.ImageFromStartFormFiltered = Colors.RgbToBitmap(FilteredHSVImage);
                            break;
                        }
                    case 3:
                        {
                            byte[, ,] FilteredYCbCrImage = BodyRecogn.YCbCrDetection(Colors.BitmapToByteRgb(capture));
                            Helper.ImageFromStartFormFiltered = Colors.RgbToBitmap(FilteredYCbCrImage);
                            break;
                        }
                }
            }

            Filtration.Processing();
            BodyRecogn.Allocation();
            if (Helper.IsTrainedNNMP)
            {
                Recognition.Recognize();
            }
            if (Helper.IsTrainedNNHopfild)
            {
                Recognition.RecognizeHopfild();
            }
            StartForm.SelfRef.UpdateStartFormPB();

            if (Helper.IsActiveColorsSettingsForm)
            {
                ColorsSettingsForm.SelfRef.UpdateColorSettFrFormPB();
            }
            if (Helper.IsActiveTrainingForm)
            {
                TrainingForm.SelfRef.UpdateTrainingFormPB();
            }
        }

        # region Стандартный функционал камеры
        public void Start()
        {
            camera.TimeToCapture_milliseconds = frameNumber;
            camera.Start(0);            
        }
        public void Stop()
        {
            camera.Stop();
            //flag = 1;
        }
        public void Continue()
        {
            camera.TimeToCapture_milliseconds = frameNumber;
            camera.Start(this.camera.FrameNumber);
        }
        public void ResolutionSetting()
        {
            camera.Config();
        }
        public void AdvanceSetting()
        {
            camera.Config2();
        }
        # endregion
    }
}
